{extends 'admin/layout/table-main.tpl'}

{block title}权限管理{/block}
{block smallTitle}系统内的权限管理。{/block}

{block main}
<div class="row">
	<div class="col-sm-12">
		<div class="ibox float-e-margins">
			<div class="ibox-title">
				<h5>角色管理 <small>后台角色编辑/添加</small></h5>
			</div>
			<div class="ibox-content">
				<form method="post" class="form-horizontal" action="{route('AdminRoleEditAction')}">
					{if $data}
					<div class="form-group">
						<label class="col-sm-2 control-label">#</label>
						<div class="col-sm-10">
							<p class="form-control-static">{$data.id}</p>
						</div>
					</div>
					{/if}
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 control-label">角色名</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" name="name" value="{Input::old('name', $data.name)|escape}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<label class="col-sm-2 control-label">备注</label>
						<div class="col-sm-10">
							<textarea type="text" class="form-control" name="remark" >{Input::old('remark', $data.remark)|escape}</textarea>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="control-group" >
						<label class="col-sm-2 control-label"><input type="checkbox" class="check_all"><th>全选</th></label>
						<div style="height: 500px; overflow-y:scroll;">
							<table class="table table-striped table-hover" id="menubox">
								<tbody >
								{foreach $menu as $key=> $item}
									<tr  level="{$item.level}" pid="{$item.pid}">
										<td width="20"><input  {if $data->menu_array&&in_array($item.id,$data->menu_array)}checked{/if} type="checkbox" class="menu" value="{$item.route}" name="menu[{$item.id}]">  </td>
										<td>{$item.option}</td>
									</tr>
								{/foreach}
								</tbody>
							</table>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="form-group">
						<div class="col-sm-4 col-sm-offset-2">
							<input type="hidden" class="form-control" name="id" value="{$data.id}">
							<button class="btn btn-primary" type="submit">保存内容</button>
							<a class="btn btn-white" type="button" href="{route('AdminRoleList')}">返回</a>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
{/block}

{block script}
	<script type="text/javascript">
		$(".check_all").click(function(){
			if($(this).attr("checked")=='checked'){
				$(".menu").attr("checked",true);
				$('#menubox span').addClass('checked');
			}else{
				$(".menu").attr("checked",false);
				$('#menubox  span').removeClass('checked');
			}
		});

		$('.menu').click(function(){
			var tr = $(this).parents('tr:first');
			var level = tr.attr('level');
			var pid = tr.attr('pid');

			if($(this).attr("checked")=='checked'){
				$(tr.nextAll('tr')).each(function(){
					if($(this).attr('level')<=level){
						return false;
					}
					$(this).find('.menu').attr("checked",true);
					$(this).find('span').addClass('checked');
				});

				while(pid>0){
					$('[name="menu['+pid+']"]').attr("checked",true);
					$('[name="menu['+pid+']"]').parent('span').addClass('checked');
					var pid =   $('[name="menu['+pid+']"]').parents('tr:first').attr('pid');
				}

			}else{
				$(tr.nextAll('tr')).each(function(){
					if($(this).attr('level')<=level){
						return false;
					}
					$(this).find('.menu').attr("checked",false);
					$(this).find('span').removeClass('checked');
				});
			}
		});
	</script>
{/block}
